<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.1.2/echarts.min.js"></script>
  <title>柱状图的基本实现 </title>
</head>

<body>
  <div style="width: 600px;height: 400px;"></div>
  <button id="btn">修改数据</button>
  <button id="add">增加数据</button>
  <script>
    var mCharts = echarts.init(document.querySelector('div'));
    var xDataArr = ['张三', '李四', '王五', '小明', '小红', '小张'];
    var yDataArr = [100, 40, 80, 26, 88, 70]

    // 如果想要横向柱形图 只要把xy轴对调即可
    //  设置配置项
    var options = {
      xAxis: {
        type: 'category',
        data: xDataArr
      },
      yAxis: {
        type: 'value'
      },
      series: [{
        name: '数学',
        type: 'bar',
        // 标记最大值最小值
        markPoint: {
          data: [{
            type: 'max',
            name: '最大值'
          }, {
            type: 'min',
            name: '最小值'
          }]
        },
        // 标记平均值
        markLine: {
          data: [{
            type: 'average',
            name: '平均值'
          }]
        },
        // 显示数据
        label: {
          show: true,
          // position: 'top'
          rotate: 60
        },
        // 柱宽
        barWidth: '30%',
        data: yDataArr
      }]
    }

    mCharts.setOption(options)

    var btn = document.querySelector('#btn');
    btn.onclick = function() {
      var newYDataArr = [10, 20, 60, 26, 100, 70];
      options = {
        series: [{
          data: newYDataArr
        }]
      };
      mCharts.setOption(options)
    }

    var add = document.querySelector('#add');
    add.onclick = function() {
      xDataArr.push('小肖');
      yDataArr.push(100);
      options = {
        xAxis: {
          data: xDataArr
        },
        series: [{
          data: yDataArr
        }]
      };
      mCharts.setOption(options)
    }
  </script>
</body>

</html>